package com.fine.demo.temperaturepolyline

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class TemperatureAdapter(context: Context, weathers: MutableList<Weather>): RecyclerView.Adapter<TemperatureAdapter.WeatherViewHolder>() {

    private val mContext: Context = context

    private val mWeathers: MutableList<Weather> = weathers

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WeatherViewHolder {
        val view: View = LayoutInflater.from(mContext).inflate(R.layout.weather_item, parent, false)
        return WeatherViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: WeatherViewHolder, position: Int) {
        val weather = mWeathers[position]
        holder.tvCity.text = weather.city
        holder.tvWeather.text = weather.weather
        holder.tvTemp.text = "${weather.maxTemp}°C - ${weather.minTemp}°C"
    }

    override fun getItemCount(): Int {
        return mWeathers.size
    }

    class WeatherViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val tvCity: TextView by lazy {
            view.findViewById<TextView>(R.id.tv_city)
        }
        val tvWeather: TextView by lazy {
            view.findViewById<TextView>(R.id.tv_weather)
        }
        val tvTemp: TextView by lazy {
            view.findViewById<TextView>(R.id.tv_temp)
        }
    }
}